package com.nursinghome.emp.service.impl;

import com.nursinghome.bean.Holidays;
import com.nursinghome.emp.mapper.EmpHolidaysMapper;
import com.nursinghome.emp.service.EmpHolidaysService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class EmpHolidaysServiceImpl implements EmpHolidaysService {
    @Autowired
    private EmpHolidaysMapper empHolidaysMapper;
    @Override
    public List<Holidays> selectByMid(Integer mid) {
        return empHolidaysMapper.selectByMid(mid);
    }

    @Override
    public Boolean insert(Holidays holidays) throws NurisingHomeException {
        Date startDate = holidays.getStartDate();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = format.format(startDate);
        //判断当前时间的该员工请假是否冲突
        if (empHolidaysMapper.selectByData(data, holidays.getMid()) != null) {
            throw  new NurisingHomeException("当前时段您已经请过一次假了！");
        }
        return empHolidaysMapper.insert(holidays) == 1;
    }

    @Override
    public Holidays selectByData(String data, Integer mid) {
        return selectByData(data,mid );
    }
}
